/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.Bookmark;
import lt.marsrutai.mobile.android.model.TransportStopBookmark;
import lt.marsrutai.mobile.android.task.GetArrivalTimesTask;
import lt.marsrutai.mobile.android.util.StringUtils;
import lt.marsrutai.mobile.android.util.TimeUntilNextDeparture;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TransportStopBookmarkAdapter extends StopBookmarkAdapter<TransportStopBookmark> {

    public TransportStopBookmarkAdapter(Context context) {
        super(context, R.layout.bookmark_transport_stop_item);
    }

    @Override
    public View doGetView(final int position, View view, final ViewGroup parent) {
        TransportStopBookmark stopBookmark = getItem(position);

        if (stopBookmark != null) {
            TextView scheduleNumber = findViewById(view, R.id.schedule_number);
            scheduleNumber.setText(null);
            scheduleNumber.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(stopBookmark.getScopeFolder(),
                    stopBookmark.getTransportId()));

            if (!StringUtils.equalsIgnoreCase(stopBookmark.getScheduleName(), stopBookmark.getScheduleTrackName())) {
                scheduleNumber.setText(stopBookmark.getScheduleName());
            }
        }

        return super.doGetView(position, view, parent);
    }
    
    protected BookmarkTask startTask(View view, TransportStopBookmark bookmark) {
        GetTransportStopArrivalTimesTask task = new GetTransportStopArrivalTimesTask(view, bookmark);
        task.execute(new GetTransportStopArrivalTimesTask.Criteria(bookmark.getScopeFolder(), bookmark.getStopName(), bookmark.getStopId(),
                bookmark.getScheduleId(), 600, 20));
        
        return task;
    }
    
    protected void clearReusedViewInfo(View view) {
        TextView nextStopTimes = (TextView) view.findViewById(R.id.next_stop_times);
        nextStopTimes.setText(StringUtils.EMPTY);
        nextStopTimes.setTextColor(getContext().getResources().getColor(R.color.black));

        TextView timeRemaining = (TextView) view.findViewById(R.id.time_remaining);
        timeRemaining.setText(AppContext.getApplicationContext().getString(R.string.arrives_in,
                new TimeUntilNextDeparture(Collections.<ArrivalTime>emptyList()).toString()));
        timeRemaining.setTextColor(getContext().getResources().getColor(R.color.black));
    }

    private class GetTransportStopArrivalTimesTask extends GetArrivalTimesTask implements BookmarkTask {

        private final WeakReference<View> textViewReference;
        private TransportStopBookmark bookmark;

        public GetTransportStopArrivalTimesTask(View view, TransportStopBookmark stopBookmark) {
            this.textViewReference = new WeakReference<View>(view);
            this.bookmark = stopBookmark;
        }

        @Override
        protected void onPostExecute(List<ArrivalTime> arrivalTimes) {
            if (isCancelled()) {
                return;
            }

            arrivalTimeMap.put(bookmark, arrivalTimes);

            if (textViewReference != null) {
                View view = textViewReference.get();
                
                if (view != null) {
                    Tag tag = getTag(view);
                    if (tag != null && tag.task == this) {
                        populateArrivalTimeInfo(view, bookmark, arrivalTimes);
                    }
                }
            }
        }
        
        @Override
        public Bookmark getBookmark() {
            return bookmark;
        }
    }

}
